package com.example.myapplication.tack_21;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.tack_21.adapter.DectorAdapter;
import com.example.myapplication.tack_21.data.DectorData;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;
import com.example.myapplication.utils.StateData;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class DectorXQActivity extends AppCompatActivity {
    private ImageView exit;
    private ImageView img;
    private TextView name;
    private TextView jobName;
    private TextView practiceNo;
    private TextView submit;
    private EditText edi;
    private JSONObject jsonObject;
    private StateData stateData;
    private Gson gson;
    private void init() {

        exit = findViewById(R.id.exit);
        img = findViewById(R.id.img);
        name = findViewById(R.id.name);
        jobName = findViewById(R.id.jobName);
        practiceNo = findViewById(R.id.practiceNo);
        submit = findViewById(R.id.submit);
        edi = findViewById(R.id.edi);
        jsonObject = new JSONObject();
        gson = new Gson();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_21_activity_dector_x_q);
        init();
        exit_();
        Intent intent = getIntent();
        String stringExtra = intent.getStringExtra(Common.COMMON);
        DectorData.RowsDTO dectorData = gson.fromJson(stringExtra, DectorData.RowsDTO.class);
        getDector(dectorData.getTypeId());
        cilakSubmit(dectorData);
    }

    private void cilakSubmit(DectorData.RowsDTO dectorData) {
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String s = edi.getText().toString();
                        try {
                            jsonObject.put("doctorId",dectorData.getId());
                            jsonObject.put("question",s);
                            jsonObject.put("imageUrls",dectorData.getAvatar());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String json = jsonObject.toString();
                        submit_(json);
                    }
                });
            }
        });
    }

    private void submit_(String json) {
        String url = "prod-api/api/pet-hospital/inquiry";
        OkHttpTool.doNetRequest(url, OkHttpTool.POST, StateData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                stateData = (StateData)obj;

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(stateData.getCode() == 200){
                            Toast.makeText(DectorXQActivity.this, stateData.getMsg(),Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(DectorXQActivity.this, stateData.getMsg(),Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        },json);
    }

    private void getDector(int id) {
        String url = "prod-api/api/pet-hospital/pet-doctor/list?typeId="+id+"&pageSize=1&pageNum=1";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, DectorData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                DectorData data = (DectorData)obj;
                if(data.getCode() == 200){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Glide.with(DectorXQActivity.this).load(OkHttpTool.URL+data.getRows().get(0).getAvatar()).into(img);
                            name.setText(data.getRows().get(0).getName());
                            jobName.setText(data.getRows().get(0).getJobName());
                            practiceNo.setText(data.getRows().get(0).getPracticeNo());

                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }



    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DectorXQActivity.this.finish();
            }
        });
    }

}